/*
 * menu.c
 *
 *  Created on: Feb 24, 2023
 *      Author: Junhoo
 */

#include "menu.h"
#include "key.h"
#include "ST7735.h"
#include <stdio.h>
#include "DAP.h"
#include "menu.h"
#include <stdlib.h>
#include "ff.h"
#include "exfuns.h"
#include "fattester.h"
#include "string.h"
#include "lcd_menu.h"
#include "hex2bin.h"
#include "SWD_host.h"
#include "SWD_flash.h"
uint32_t Flash_Page_Size = 1024;
uint32_t Flash_Start_Addr = 0x08000000;
uint8_t buff[1024] = {0};

uint8_t app_mode=0;
char Name_Buffer[20][128];
int8_t file_name = 0, name_cnt = 0;

uint8_t Cheack_Device_Con();




#if 0
#define X0 15
uint8_t Select_Mode()
{
	ST7735_WriteString(X0,Y2,"Online DAP", Font_7x10, ST7735_WHITE, ST7735_BLACK);
	ST7735_WriteString(X0,Y3,"Offline DAP", Font_7x10, ST7735_WHITE, ST7735_BLACK);
	ST7735_WriteString(X0,Y4,"Auto DAP", Font_7x10, ST7735_WHITE, ST7735_BLACK);

	static uint8_t choose = Y3;
	if (g_keyPressIdx == Key_Down)
	{
		g_keyPressIdx = -1;
		choose += 11;
		if (choose >= Y4)
			choose = Y4;
//		OLED_ShowString(0, choose - 11, "  ", 11, 1);
		ST7735_WriteString(0,choose - 11,"  ", Font_7x10, ST7735_WHITE, ST7735_BLACK);
	}
	else if (g_keyPressIdx == Key_Up)
	{
		g_keyPressIdx = -1;
		choose -= 11;
		if (choose < Y2)
			choose = Y2;
//		OLED_ShowString(0, choose + 11, "  ", 11, 1);
		ST7735_WriteString(0,choose + 11,"  ", Font_7x10, ST7735_WHITE, ST7735_BLACK);
	}
	else if (g_keyPressIdx == Key_Right)
	{
		g_keyPressIdx = -1;
		LCD_ClearMenu();
		if (choose == Y2)
		{
			app_mode = MODE_SET_ONLINE;

		}
		else if (choose == Y3)
		{
			app_mode = MODE_SET_OFFLINE;
		}
		if (choose == Y4)
		{
			app_mode = MODE_SET_TEST;
			ST7735_WriteString(X0,Y2,"Test DAP", Font_7x10, ST7735_WHITE, ST7735_BLACK);
//			for (uint8_t i = 0; i < 12; i++)
//				OLED_Show_CH_String(Line1_Begin + i * 6, Y2, oled_CH2[i], 12, 1);
		}
		return app_mode;
	}

	ST7735_WriteString(0, choose, ">>", Font_7x10, ST7735_WHITE, ST7735_BLACK);
	return MODE_SET_MENU;
}
#endif

void DAP_Online()
{
//ST7735_WIDTH  160
//ST7735_HEIGHT 80
	static int x = 25;
	static int y = 31;
	ST7735_WriteString(x,y,"Online DAP", Font_11x18, ST7735_WHITE, ST7735_BLACK);
	 switch(g_keyPressIdx)
	 {
		 case Key_Letf:
			app_mode=MODE_SET_MENU;
			g_keyPressIdx = -1;
			break;
		 case Key_Right:
			g_keyPressIdx = -1;
			break;
		 case Key_Down:
			g_keyPressIdx = -1;
			break;
		 case Key_Up:
			g_keyPressIdx = -1;
			break;
//		 default:break;

	 }
}


enum
{
	DAP_RELEASE = 0,
	DAP_RCONN,
	DAP_FLASH,
	DAP_FCONN,
};
void DAP_Auto()
{
	ST7735_WriteString(1,1,"DAP Auto", Font_11x18, ST7735_BLUE, ST7735_BLACK);
	static uint8_t dap_status=DAP_RELEASE;
	static uint8_t dap_last_status=DAP_RELEASE;
	switch(g_keyPressIdx)
	{
		 case Key_Letf:
			dap_status=DAP_RELEASE;
			dap_last_status=DAP_RELEASE;
			app_mode=MODE_SET_MENU;
			g_keyPressIdx = -1;
			break;
		 case Key_Right:
			g_keyPressIdx = -1;
			break;
		 case Key_Down:
			g_keyPressIdx = -1;
			break;
		 case Key_Up:
			g_keyPressIdx = -1;
			break;
//		 default:break;

	}

//	static int tcont = 500;
//	if(tcont == 500){
//	tcont = 0;
	static uint8_t dap_con;
	switch(dap_status)
	{
	 	 case DAP_RELEASE:
	 		dap_con = Cheack_Device_Con();
	 		if(dap_con)
	 			dap_status = DAP_RCONN;
	 		 break;
	 	 case DAP_RCONN:
	 		HAL_Delay(1000);
	 		dap_con = Cheack_Device_Con();
	 		if(!dap_con)
	 			dap_status = DAP_RELEASE;
	 		else dap_status = DAP_FLASH;
	 		 break;
	 	 case DAP_FLASH:
	 		 LCD_ClearMenu();
	 		if(swd_flash())
	 		{
	 			ST7735_WriteString(1,19,"Flash PASS", Font_11x18, ST7735_GREEN, ST7735_BLACK);
	 		}
	 		else
	 			ST7735_WriteString(1,19,"Flash FAIL", Font_11x18, ST7735_RED, ST7735_BLACK);
	 		dap_status = DAP_FCONN;

	 		 break;
	 	 case DAP_FCONN:
			dap_con = Cheack_Device_Con();
			if(!dap_con)
			{
		 		LCD_ClearMenu();
				dap_status = DAP_RELEASE;
			}
	 		 break;
	 	 default:break;
	}
	if(dap_last_status != dap_status)
	{
		 printf("last = %d now = %d\r\n",dap_last_status,dap_status);
		 dap_last_status = dap_status;
	}
//	}
//	HAL_Delay(100);
//	tcont++;


}


void DAP_Offline()
{
	static uint8_t fRefresh=1;
	static uint8_t s_MenuInit = 0;
	static MENU_T g_tMenu_FLM;
	static char *g_Menu_BIN_Text[128]; //static避免再次进入函数重新定义
	if (s_MenuInit == 0)
	{
		name_cnt = 0;
		memset(g_Menu_BIN_Text, 0, sizeof(g_Menu_BIN_Text));
		if (f_opendir(&dir, (const TCHAR *)"1:") == FR_OK)
		{
			f_readdir(&dir, &fileinfo);
			while (f_readdir(&dir, &fileinfo) == FR_OK)
			{
				if (!fileinfo.fname[0])
				{
					for (uint8_t i = 0; i < name_cnt; i++)
					{
						g_Menu_BIN_Text[i] = Name_Buffer[i];
					}
					g_Menu_BIN_Text[name_cnt] = "&";
					break;
				}
				strcpy(Name_Buffer[name_cnt], "1:/");
				strcat(Name_Buffer[name_cnt],fileinfo.fname);
				name_cnt++;
			}
		}
		else
		{
			g_Menu_BIN_Text[0] = "open error!!!";
			g_Menu_BIN_Text[1] = "&";
		}

		s_MenuInit = 1;
		g_tMenu_FLM.Left = MENU_LEFT;
		g_tMenu_FLM.Top = MENU_TOP;
		g_tMenu_FLM.LineCap = MENU_CAP;
		g_tMenu_FLM.Font = &Font_7x10;
		g_tMenu_FLM.ViewLine = MENU_HEIGHT / g_tMenu_FLM.Font->height;
		g_tMenu_FLM.RollBackEn = 1;
		LCD_InitMenu(&g_tMenu_FLM, (char **)g_Menu_BIN_Text);
	}

	if (fRefresh)
	{
		fRefresh = 0;
		LCD_DispMenu(&g_tMenu_FLM);
	}

	 switch(g_keyPressIdx)
	 {
		 case Key_Letf:
			printf("letf\r\n");
			fRefresh = 1;
			app_mode=MODE_SET_MENU;
			g_keyPressIdx = -1;
			break;
		 case Key_Right:
//			OLED_Clear();
			printf("%s\r\n",g_tMenu_FLM.Text[g_tMenu_FLM.Cursor]);
			if( HexFile2BinFile(g_tMenu_FLM.Text[g_tMenu_FLM.Cursor], "1:/test.bin") == F_RES_OK)
			{
				LCD_ClearMenu();
				ST7735_WriteString(0,0,"HexFile2BinFile Success", Font_7x10, ST7735_WHITE, ST7735_BLACK);
				printf("HexFile2BinFile Success\r\n");
				if ( swd_flash() )
				{
					ST7735_WriteString(0,11,"Flash Success", Font_7x10, ST7735_WHITE, ST7735_BLACK);
				}
				else ST7735_WriteString(0,11,"Flash Fail", Font_7x10, ST7735_WHITE, ST7735_BLACK);
			}
			else
			{
				LCD_ClearMenu();
				ST7735_WriteString(0,0,"HexFile2BinFile Fail", Font_7x10, ST7735_WHITE, ST7735_BLACK);
			}
			fRefresh = 0;
			g_keyPressIdx = -1;
			break;
		 case Key_Down:
			LCD_ClearMenu();
			LCD_MoveDownMenu(&g_tMenu_FLM);
			g_keyPressIdx = -1;
			break;
		 case Key_Up:
			LCD_ClearMenu();
			LCD_MoveUpMenu(&g_tMenu_FLM);
			g_keyPressIdx = -1;
			break;
//		 default:break;

	 }

}

uint8_t swd_flash()
{
	error_t error;


	if( (error = target_flash_init(Flash_Start_Addr)) != 0)
	{
		printf("target_flash_init error %d\r\n",error);
		return 0;
	}
	printf("target_flash_init !!!!!!!!!!!!!!\r\n");


	if( (error = target_flash_erase_chip()) != 0)
	{
		printf("target_flash_erase_sector error %d\r\n",error);
		return 0;
	}


//	for(uint32_t addr = 0; addr < sizeof(test); addr += 1024)
//	{
//		swd_read_memory(0x08000000 + addr, buff, 1024);
//		for(uint32_t i = 0; i < 1024; i++)  printf("%02X ", buff[i]);
//		printf("\r\n\r\n\r\n");
//	}

//	for(uint32_t addr = 0; addr < sizeof(test); addr += 1024)
//	{
//		target_flash_program_page(0x08000000+addr, &test[addr], 1024);
//	}

//	FRESULT Res;
	f_open(ftemp, "1:test.bin", FA_READ);
	uint8_t flash_buf[1024];
	uint32_t addr = 0;
	while(1)
	{
		memset(flash_buf,0,1024);
		f_read(ftemp,flash_buf,1024,&br);
		if(br==0) break;
		printf("br=%d\r\n",br);

		if( (error = target_flash_program_page(0x08000000+addr, flash_buf, 1024) != 0) )
		{
			printf("target_flash_program_page error %d\r\n",error);
			return 0;
		}
		addr+=1024;

	}
	f_close(ftemp);

//	for(uint32_t addr = 0; addr < sizeof(test); addr += 1024)
//	{
//		swd_read_memory(0x08000000 + addr, buff, 1024);
//		for(uint32_t i = 0; i < 1024; i++) printf("%02X ", buff[i]);
//		printf("\r\n\r\n\r\n");
//	}
	swd_set_target_state_hw(RUN);
	return 1;

}

uint8_t Cheack_Device_Con()
{

	uint32_t id;
	if(read_id(&id))
	{
//		printf("ID = %lX\r\n",id);
		return 1;
	}


#if 0
	//GPIO init
	  GPIO_InitTypeDef GPIO_InitStruct = {0};

	  /* GPIO Ports Clock Enable */
	  __HAL_RCC_GPIOB_CLK_ENABLE();

	  /*Configure GPIO pin : PtPin */
	  GPIO_InitStruct.Pin = swd_clk_Pin;
	  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
	  GPIO_InitStruct.Pull = GPIO_NOPULL;
	  HAL_GPIO_Init(swd_clk_GPIO_Port, &GPIO_InitStruct);

	  /*Configure GPIO pin : PtPin */
	  GPIO_InitStruct.Pin = swd_io_Pin;
	  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
	  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
	  HAL_GPIO_Init(swd_io_GPIO_Port, &GPIO_InitStruct);
	//read
	uint32_t id;
	if(HAL_GPIO_ReadPin(swd_io_GPIO_Port, swd_io_Pin))//IO�ߵ�ƽ
	{
		if(0==HAL_GPIO_ReadPin(GPIOB, swd_clk_Pin))//CLK�͵�ƽ
		{
			printf("��������\r\n");
			if(read_id(&id))
			{
				printf("ID = %lX\r\n",id);
				return 1;
			}
		}
	}
#endif


	return 0;
}
